using System;
using System.Data;
using System.Collections;
using CE.Database;
/// <summary>
/// Summary description for CarDetailDB
/// </summary>
/// 
namespace CE.CarInsurance
{
    public class CarAccessaryDB
    {
        public CarAccessaryDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarAccessary caraccessary)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCarAccessaryInsertUpdateSingleItem";
            
            SortedList _sPara = new SortedList();
            
            #region Parameter
            _sPara.Add("@CarAccessaryId", caraccessary.CarAccessaryId);
            _sPara.Add("@CECarId", caraccessary.CECarId);
            _sPara.Add("@AccDescription", caraccessary.AccDescription);
            _sPara.Add("@AccValue", caraccessary.AccValue);
            _sPara.Add("@RefCEUserId", caraccessary.RefCEUserId);
            _sPara.Add("@ViewedDetailId", caraccessary.ViewedDetailId);
            _sPara.Add("@Status", caraccessary.Status);
            _sPara.Add("@Info1", caraccessary.Info1);
            _sPara.Add("@Info2", caraccessary.Info2);
            _sPara.Add("@Info3", caraccessary.Info3);
            _sPara.Add("@Info4", caraccessary.Info4);
            _sPara.Add("@Info5", caraccessary.Info5);
            _sPara.Add("@Info6", caraccessary.Info6);
            _sPara.Add("@Info7", caraccessary.Info7);
            _sPara.Add("@Info8", caraccessary.Info8);
            _sPara.Add("@Info9", caraccessary.Info9);
            _sPara.Add("@Info10", caraccessary.Info10);
                
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete()
        {
            return true;
        }

        public CarAccessary GetCarAccessary(Int64 CarId, Int64 CEUserId)
        {
            CarAccessary _caraccessary = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarAccessarySelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CECarId", CarId);
            _spara.Add("@RefCEUserId", CEUserId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caraccessary = new CarAccessary();
                    _caraccessary = FillDataRecord(_dt, 0);
                }
            }
            return _caraccessary;
        }

        public CarAccessaryList GetCarAccessaryList(Int64 CarId, Int64 CEUserId)
        {
            CarAccessaryList _caraccessarylist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarAccessarySelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@CECarID", CarId);
            _spara.Add("@RefCEUserId", CEUserId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName,_spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caraccessarylist = new CarAccessaryList();
                    int _rowcount = _dt.Rows.Count;
                    for (int i = 0; i < _rowcount; i++)
                    {
                        CarAccessary _caraccessary = new CarAccessary();
                        _caraccessary = FillDataRecord(_dt, i);
                        _caraccessarylist.Add(_caraccessary);
                    }
                }
            }
            return _caraccessarylist;
        }

        #region Comment
        //public CarDetail GetCar(string GUID)
        //{
        //    CarDetail _cardetail = null;
        //    MyDatabase _db = new MyDatabase();
        //    string _spName = "sprocCarDetailByGUIDSelectSingleItem";

        //    SortedList _spara = new SortedList();
        //    _spara.Add("@GUID", GUID);

        //    using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
        //    {
        //        if (_dt != null && _dt.Rows.Count > 0)
        //        {
        //            _cardetail = new CarDetail();
        //            _cardetail = FillDataRecord(_dt);
        //        }
        //    }
        //    return _cardetail;
        //}

        //public string GetGUID(Int64 CarId)
        //{
        //    MyDatabase _db = new MyDatabase();
        //    string strSQL = "Select ROWGUID from tblCarDetail Where [CECarId] = @CECarID";

        //    SortedList _spara = new SortedList();
        //    _spara.Add("@CECarID", CarId);

        //    object _result = _db.ExecuteScalarSqlStatement(strSQL, _spara);

        //    return _result.ToString();
        //}
        #endregion

        private CarAccessary FillDataRecord(DataTable myDataTable, int rowId)
        {
            CarAccessary myCarAccessary = new CarAccessary();
            myCarAccessary.CarAccessaryId = Convert.ToInt64(myDataTable.Rows[rowId]["CarAccessaryId"].ToString());
            myCarAccessary.CECarId = Convert.ToInt32(myDataTable.Rows[rowId]["CECarId"].ToString());
            myCarAccessary.AccDescription = myDataTable.Rows[rowId]["AccDescription"].ToString();
            myCarAccessary.AccValue = Convert.ToSingle(myDataTable.Rows[rowId]["AccValue"].ToString());
            myCarAccessary.RefCEUserId = Convert.ToInt64(myDataTable.Rows[rowId]["RefCEUserId"].ToString());
            myCarAccessary.Status = myDataTable.Rows[rowId]["Status"].ToString();
            return myCarAccessary;
        }
    }
}